package com.bighero.mq_to_mail.pAndC;


import com.bighero.mq_to_mail.pAndC.utils.SendEmailUtil;
import com.bighero.mq_to_mail.pAndC.utils.Tool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@Component

public class ConsumerToMail {

    @Autowired
    RabbitTemplate rabbitTemplate;


    @RabbitListener(queues = "simple.queue")
    public void consumerMsg(String msg){
        String pattern = "\\{[^{}]+\\}";

        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(msg);

        String qqNumber = "";
        while (m.find()) {
            qqNumber = Tool.ALL_MAIL.get(m.group());
        }
        try {
            SendEmailUtil.sendEmail(qqNumber,"飞哥天气预报,祝您平安",msg);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        System.out.println(qqNumber + "....收到...："+msg);
    }
}
